package org.study.jackson.deserialize;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.ResolvedType;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.val;
import org.study.jackson.bean.Person;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;

public class MyObjectCodec extends ObjectCodec {
    @Override
    public <T> T readValue(JsonParser parser, Class<T> valueType) throws IOException {
        Person person = new Person();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            if (parser.currentToken() == JsonToken.FIELD_NAME) {
                switch (parser.getCurrentName()) {
                    case "name":
                        parser.nextToken();
                        person.name = parser.getValueAsString();
                        break;
                    case "age":
                        parser.nextToken();
                        person.age = parser.getValueAsInt();
                        break;
                    case "girlFriend":
                        parser.nextToken();
                        val girlFriend = new Person();
                        person.girlFriend = girlFriend;
                        while (parser.nextToken() != JsonToken.END_OBJECT) {
                            switch (parser.getCurrentName()) {
                                case "name":
                                    parser.nextToken();
                                    girlFriend.name = parser.getValueAsString();
                                    break;
                                case "age":
                                    parser.nextToken();
                                    girlFriend.age = parser.getValueAsInt();
                                default:
                            }
                        }
                        break;
                    case "hobbies":
                        parser.nextToken();
                        val hobbies = new LinkedList<String>();
                        person.hobbies = hobbies;
                        while (parser.nextToken() != JsonToken.END_ARRAY) {
                            hobbies.add(parser.getValueAsString());
                        }
                        break;
                    default:
                }
            }
        }

        return (T) person;
    }

    @Override
    public Version version() {
        return null;
    }

    @Override
    public <T> T readValue(JsonParser p, TypeReference<T> valueTypeRef) throws IOException {
        return null;
    }

    @Override
    public <T> T readValue(JsonParser p, ResolvedType valueType) throws IOException {
        return null;
    }

    @Override
    public <T> Iterator<T> readValues(JsonParser p, Class<T> valueType) throws IOException {
        return null;
    }

    @Override
    public <T> Iterator<T> readValues(JsonParser p, TypeReference<T> valueTypeRef) throws IOException {
        return null;
    }

    @Override
    public <T> Iterator<T> readValues(JsonParser p, ResolvedType valueType) throws IOException {
        return null;
    }

    @Override
    public void writeValue(JsonGenerator gen, Object value) throws IOException {

    }

    @Override
    public <T extends TreeNode> T readTree(JsonParser p) throws IOException {
        return null;
    }

    @Override
    public void writeTree(JsonGenerator gen, TreeNode tree) throws IOException {

    }

    @Override
    public TreeNode createObjectNode() {
        return null;
    }

    @Override
    public TreeNode createArrayNode() {
        return null;
    }

    @Override
    public JsonParser treeAsTokens(TreeNode n) {
        return null;
    }

    @Override
    public <T> T treeToValue(TreeNode n, Class<T> valueType) throws JsonProcessingException {
        return null;
    }
}
